package com.example.backetballgame.gui;


import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * The WelcomeFrame class is the initial frame of the game.
 * It displays the author name, game name, and exit label.
 * The user can press the space key to close the window and create a new window,
 * or press the ESC key to exit the program.
 */
public class WelcomeFrame extends JFrame implements KeyListener {

    private JLabel authorLabel; // Author name label
    private JLabel gameLabel; // Game name label
    private JButton exitLabel; // Exit button

    public WelcomeFrame() {
        super("Welcome to the Game World"); // Set window title
        initFrame(); // Initialize window
    }

    /**
     * Initializes the window by setting its size, default close operation, panel, keyboard event listener, and visibility.
     */
    public void initFrame() {
        setSize(800, 600); // Set window size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set default operation when closing window

        JPanel panel = createPanel();

        // Add keyboard event listener
        addKeyListener(this);
        setFocusable(true);

        // Add panel to center of window
        getContentPane().add(panel, BorderLayout.CENTER);

        exitLabel.addActionListener(e -> System.exit(0));

        setVisible(true); // Display window
    }

    /**
     * Creates a panel with labels and sets their fonts, colors, and layout constraints.
     *
     * @return A JPanel object with author label, game name label, continue label, and exit label.
     */
    public JPanel createPanel() {
        // Create panel and layout manager
        JPanel panel = new JPanel(new GridBagLayout());

        // Create labels
        authorLabel = new JLabel("Author: Shengqin Xu, Beifang Yu");
        gameLabel = new JLabel("Game Name: Sports Elimination Tournament");
        // "Press space to continue" label
        JLabel continueLabel = new JLabel("Press space to continue");
        exitLabel = new JButton("Exit");

        // Set font and color
        authorLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        gameLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        continueLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        exitLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        authorLabel.setForeground(Color.GRAY);
        gameLabel.setForeground(Color.BLUE);
        continueLabel.setForeground(Color.BLACK);
        exitLabel.setForeground(Color.BLACK);

        // Add components to panel and set constraint relationship
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.weightx = 1;
        c.weighty = 1;
        c.gridx = 0;
        c.gridy = 0;
        panel.add(authorLabel, c);
        c.gridx = 0;
        c.gridy = 1;
        panel.add(gameLabel, c);
        c.gridx = 0;
        c.gridy = 2;
        panel.add(continueLabel, c);
        c.gridx = 0;
        c.gridy = 3;
        panel.add(exitLabel, c);
        return panel;
    }

    /**
     * Responds to the user pressing a key.
     * If the user presses the space key, the window is closed and a new window is created.
     * If the user presses the ESC key, the program exits.
     *
     * @param e The KeyEvent object.
     */
    @Override
    public void keyPressed(KeyEvent e) {
        // Press space to close window and create new window
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            dispose(); // Close window
            new TeamSeasonFrame(); // Create new window object
        }
        // Press ESC to exit program
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            System.exit(0); // Exit program
        }
    }

    /**
     * Responds to the user typing a key.
     * This method is not used in this class.
     *
     * @param e The KeyEvent object.
     */
    @Override
    public void keyTyped(KeyEvent e) {
    }

    /**
     * Responds to the user releasing a key.
     * This method is not used in this class.
     *
     * @param e The KeyEvent object.
     */
    @Override
    public void keyReleased(KeyEvent e) {
    }

    public static void main(String[] args) {
        WelcomeFrame welcomePage = new WelcomeFrame(); // Create window object
    }
}